extends Node

class_name FightManager

static var Inst: FightManager

func initMonsterData():
	var card = LangYao.CreateCard()
	card.data.isMonster = true;
	
	var card2 = LangYao.CreateCard()
	card.data.isMonster = true;
	
	var monsterGroup: MonsterGroup = get_node("/root/Scene/monster_group")
	monsterGroup.addCard(card, 3)
	
	monsterGroup.addCard(card2, 4)



func initData():
	initMonsterData()
	
func _ready() -> void:
	
	Inst = self
	
	initData()

var playerCards: Array
var monsterCards: Array	

var isFighting = false

func startFight():
	
	var monsterGroup: MonsterGroup = get_node("/root/Scene/monster_group")
	var playerGroup: PlayerGroup = get_node("/root/Scene/player_group")
	
	playerCards = playerGroup.cards
	monsterCards = monsterGroup.cards
	
	isFighting = true
	
	
func _process(delta: float) -> void:
	
	if isFighting:
		if playerCards.is_empty():
			showFightResult(false)
		elif monsterCards.is_empty():
			showFightResult(true)
		else:
			for i: Card in playerCards:
				if i.data.canAttack.call():
					i.data.onAttack.call(monsterCards)
		
			for i: Card in monsterCards:
				if i.data.canAttack.call():
					i.data.onAttack.call(playerCards)

var fightResultPrefab = load("res://prefab/FightResult.tscn")


func showFightResult(isWin: bool):
	isFighting = false
	
	var fightResult: FightResult = fightResultPrefab.instantiate()
	add_child(fightResult)
	
	fightResult.setResult(isWin)
	
	
	
	
